﻿using System;
using System.Configuration;
using System.Drawing;
using System.Net.Mail;
using Contacts.Data.Data;


namespace Contacts.Web
{
   public partial class Login1 : System.Web.UI.Page
   {
      private string _appconfConnString = ConfigurationManager.AppSettings["DatabaseConnection"];

      protected void Page_Load(object sender, EventArgs e)
      {

      }
   
      protected void BtnRegisterClick(object sender, EventArgs e)
      {
         ContactsDataUsers _contactDataUsers = new ContactsDataUsers();

         try
         {
            _contactDataUsers.SaveUser(txtlogin2.Text, txtPassword2.Text);
         }
         catch (Exception err)
         {
            Response.Write(err.Message);
         }
         finally
         {
            //MailMessage m = new MailMessage();
            //m.From = new MailAddress("headmasterkain@gmail.com");
            //m.To.Clear();
            //m.To.Add("ddan@sfdev.com");
            //m.IsBodyHtml = false;
            //m.BodyEncoding = System.Text.Encoding.GetEncoding("windows-1251");
            //m.Subject = "Регистрация";
            //m.Body = "Спасибо за регистрацию! \n Ваш логин " + txtlogin2.Text + " и пароль " + txtPassword2.Text;

            //SmtpClient client = new SmtpClient("127.0.0.1");
            ////client.Send(m);
            //client.SendAsync(m, null);

            lblHello.Text = "Hello " + txtlogin2.Text + " !";
         }

      }
      
      protected void BtnLoginClick(object sender, EventArgs e)
      {
         ContactsDataUsers _contactDataUsers = new ContactsDataUsers();

         int usersCount = _contactDataUsers.UsersCount(txtLogin.Text, txtPassword.Text);
         if (usersCount > 0)
         {  
            Session["Authorized"] = true;
            Response.Redirect("Default.aspx");
         }
         else
         {
            Session["Authorized"] = null;
            Msg.ForeColor = Color.Red;
            Msg.Text = "Неправильные данные: попробуйте повторить попытку";
         }

      }


   }
}